package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class LineChartDemo6 extends ApplicationFrame {
	Date sqlDate, sqlDate1;
 public ArrayList<String> array;
    public LineChartDemo6(final String title,ArrayList<String> ar, Date sqlDate, Date sqlDate1) throws ParseException {

        super(title);
        this.array=ar;
        this.sqlDate=sqlDate;
        this.sqlDate1=sqlDate1;
        final XYDataset dataset = createDataset();
      }
   private XYDataset createDataset() throws ParseException {
               
        //String[] timeStamps = new String[]{"26.08.2010","27.08.2010","27.08.2010","28.08.2010"};
        //SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        XYDataset dataset1;
        //method one: DefaultXYDataset
        /*dataset = new DefaultXYDataset();
        double[][] data = new double[2][timeStamps.length];
        for(int i = 0; i < timeStamps.length; i++){
            data[0][i] = df.parse(timeStamps[i]).getTime();
            data[1][i] = i;
        }
        ((DefaultXYDataset)dataset).addSeries("DefaultXYDataset",data);*/      
        //method two: XYSeriesCollection
        dataset1 = new XYSeriesCollection();
        XYSeries series = new XYSeries("Price", true, true); //use the allowDuplicateValues flag!
        for(int i =0;i<array.size();i=i+2)
        {
        	double value = Double.parseDouble(array.get(i+1));
        	series.add(df.parse(array.get(i)).getTime(),value);
        }
        ((XYSeriesCollection)dataset1).addSeries(series);        
        DateAxis xAxis = new DateAxis("Date");
        xAxis.setDateFormatOverride(df);
       // xAxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1));//show only one tick mark per day though there would be place for more tick marks
        ValueAxis yAxis = new NumberAxis("Values");
        XYItemRenderer renderer = new XYLineAndShapeRenderer();
        XYPlot plot = new XYPlot(dataset1, xAxis, yAxis, renderer);
        JFreeChart chart = new JFreeChart("", new Font("Tahoma", 2, 18), plot, true);
        JFrame frame = new JFrame("GRAPH");
        final JLabel lblFrom1 = new JLabel("From:");
        final JLabel lblto1 = new JLabel("From:");
        frame.setContentPane(new ChartPanel(chart));
        lblFrom1.setText("From:"+sqlDate.toString());
        frame.add(lblFrom1);
        frame.add(lblto1);
		lblto1.setText("To:"+sqlDate1.toString());
        frame.pack();
        frame.setVisible(true);
        return dataset1;
     }
    
    public void windowClosing(final WindowEvent evt)
    {
    	if(evt.getWindow() == this){
    	dispose();
    	}
    }
    
}